package com.woniuxy.intellihome.controller.ucenter;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniuxy.intellihome.entity.*;
import com.woniuxy.intellihome.service.CollectService;
import com.woniuxy.intellihome.service.SkuService;
import com.woniuxy.intellihome.service.UserService;
import com.woniuxy.intellihome.service.VCollectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author jobob。
 * @since 2021-08-11，
 */
@Controller("ucenter/collect")
@RequestMapping("ucenter/collect")
public class CollectController {
    @Autowired(required = true)
    private CollectService collectService;
    @Autowired(required = true)
    private SkuService skuService;
    @Autowired(required = true)
    private VCollectService vCollectService;

    //展示收藏夹列表
    @GetMapping("list")
    public String list(Integer current,Model model, HttpSession session,VCollect vCollect) {
        User user = (User) session.getAttribute("user1");
        //User user = new User();
       // user.setId(1);
       // List<VCollect> vcollects = vCollectService.getAllByUserId(user.getId());
        if(vCollect == null){
            vCollect = new VCollect();
        }
        QueryWrapper<VCollect> queryWrapper = new QueryWrapper<>();
        if (vCollectService.getAllByUserId(user.getId())!=null) {
            queryWrapper.eq("user_id",user.getId());
        }
        //分页跳转
        if (current == null) {
            current = 1;
        }
        Page<VCollect> page = new Page<>(current, 2);
        page = vCollectService.page(page,queryWrapper);
        System.out.println("当前页"+page.getCurrent());
        System.out.println("page里的内容"+page.getRecords());
        model.addAttribute("page", page);
        System.out.println("page========"+page);
        return "ucenter/user_Collect";
    }

//通过用户ID和SKUid删除收藏的商品
    @GetMapping("del/{skuId}")
    public String del(@PathVariable int skuId, Model model,HttpSession session){
        System.out.println("删除skuId============"+skuId);
        User user = (User) session.getAttribute("user1");
        collectService.delByUserIdWithSkuId(user.getId(),skuId);
        return "redirect:/ucenter/collect/list";//前台重定向
    }

    //通过用户ID和SKUid增加收藏的商品
    @ResponseBody
    @GetMapping("add/{skuId}")
    public ResponseEntity add(@PathVariable int skuId, HttpSession session){
        System.out.println("要增加skuId============"+skuId);
        User user = (User) session.getAttribute("user1");
        if (user==null){
           return ResponseEntity.INVALIDARGS;
        }else {
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
            System.out.println(df.format(new Date()));
            collectService.addByUserIdWithSkuId(user.getId(), skuId, df.format(new Date()));
            return ResponseEntity.SUCCESS;
        }

    }
   /* @GetMapping("add/{skuId}")
    public String add(@PathVariable int skuId, Model model,HttpSession session){
        System.out.println("要增加skuId============"+skuId);
       User user = (User) session.getAttribute("user1");
       if (user==null){
           model.addAttribute("msg","请先登录再收藏商品！");
       }else {
           SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
           System.out.println(df.format(new Date()));
           collectService.addByUserIdWithSkuId(user.getId(), skuId, df.format(new Date()));
       }
        return "redirect:/front/SPU/index";//前台重定向
    }*/

}

